App.require(["matrix3"], function (matrix3) {
    var Matrix3 = matrix3.Matrix3;
    App.defineWidget("linechart", {
        render: function (ctx) {
            ctx.save();
            var style = this.style;
            if (style.option.length == 0) return;
            ctx.globalAlpha = style.opacity;
            var shadowObj = App.Widget.strToShadow(style.shadow);
            if (shadowObj) {
                ctx.shadowOffsetX = shadowObj.x;
                ctx.shadowOffsetY = shadowObj.y;
                ctx.shadowBlur = shadowObj.blur;
                ctx.shadowColor = shadowObj.color;
            }
            ctx.beginPath();
            ctx.moveTo(style.x, style.y - style.option[0].value);
            for (var i = 1; i < style.option.length; i++) {
                var rx = style.x + i * style.distance,
                    ry = style.y - style.option[i].value;
                ctx.lineTo(rx, ry);
            }

            ctx.linewidth = style.linewidth;
            if (Array.isArray(style.linedash) && style.linedash.length > 0) {
                ctx.setLineDash(style.linedash);
                ctx.lineDashOffset = style.linedashoffset;
            }
            ctx.strokeStyle = style.stroke;
            ctx.stroke();
            ctx.restore();
        },
        createPath: function (ctx) {
            var style = this.style;
            ctx.beginPath();
            ctx.moveTo(style.x, style.y - style.option[0].value - 10);
            for (var i = 1; i < style.option.length; i++) {
                var rx = style.x + i * style.distance,
                    ry = style.y - style.option[i].value - 10;
                ctx.lineTo(rx, ry);
            }
            for (var i = style.option.length - 1; i >= 0; i--) {
                var rx = style.x + i * style.distance,
                    ry = style.y - style.option[i].value + 10;
                ctx.lineTo(rx, ry);
            }
            ctx.closePath();
        },
        isPointIn: function (ctx, x, y) {
            this.createPath(ctx);
            return ctx.isPointInPath(x, y);
        },
        style: {
            x: 0,
            y: 0,
            direction: App.AxisDirectionOption.BOTTOMTOP,
            distance: 30,
            rendertype: "",
            marker: "",
            mark: true,
            markOffset: "",
            shadow: "0 0 0 black",
            stroke: "",
            fill: "black",
            opacity: 1,
            linewidth: 1,
            linedash: "",
            linedashoffset: 0,
            percent: 1,
            progress: 1,
            option: [ { value: 20 }, { value: 40 }, { value: 35 } ]
        },
        get x() {
            return this.style.x;
        },
        set x(val) {
            this.style.x = val;
            this.getScene().nextRender();
        },
        set y(val) {
            this.style.y = val;
            this.getScene().nextRender();
        },
        get y() {
            return this.style.y;
        },
        get direction() {
            return this.style.direction;
        },
        set direction(val) {
            this.style.direction = val;
            this.getScene().nextRender();
        },
        get distance() {
            return this.style.distance;
        },
        set distance(val) {
            this.style.distance = val;
            this.getScene().nextRender();
        },
        get rendertype() {
            return this.style.rendertype;
        },
        set rendertype(val) {
            this.style.rendertype = val;
            this.getScene().nextRender();
        },
        get marker() {
            return this.style.marker;
        },
        set marker(val) {
            this.style.marker = val;
            this.getScene().nextRender();
        },
        get mark() {
            return this.style.mark;
        },
        set mark(val) {
            this.style.mark = val;
            this.getScene().nextRender();
        },
        get markOffset() {
            return this.style.markOffset;
        },
        set markOffset(val) {
            this.style.markOffset = val;
            this.getScene().nextRender();
        },
        get shadow() {
            return this.style.shadow;
        },
        set shadow(val) {
            this.style.shadow = val;
            this.getScene().nextRender();
        },
        get stroke() {
            return this.style.stroke;
        },
        set stroke(val) {
            this.style.stroke = val;
            this.getScene().nextRender();
        },
        get fill() {
            return this.style.fill;
        },
        set fill(val) {
            this.style.fill = val;
            this.getScene().nextRender();
        },
        get opacity() {
            return this.style.opacity;
        },
        set opacity(val) {
            this.style.opacity = val;
            this.getScene().nextRender();
        },
        get linewidth() {
            return this.style.linewidth;
        },
        set linewidth(val) {
            this.style.linewidth = val;
            this.getScene().nextRender();
        },
        get linedash() {
            return this.style.linedash;
        },
        set linedash(val) {
            this.style.linedash = val;
            this.getScene().nextRender();
        },
        get linedashoffset() {
            return this.style.linedashoffset;
        },
        set linedashoffset(val) {
            this.style.linedashoffset = val;
            this.getScene().nextRender();
        },
        get percent() {
            return this.style.percent;
        },
        set percent(val) {
            this.style.percent = val;
            this.getScene().nextRender();
        },
        set progress(val) {
            this.style.progress = val;
            this.getScene().nextRender();
        },
        get progress() {
            return this.style.progress;
        }
    })
})